import api from '../../api';
import { message } from 'antd';
import uuid from 'es6-uuid';
const { multiFundDivision } = api;
export default {
  namespace: 'multiFundDivisionM',
  state: {
    columns: [], //表格的表头
    tableList: [], //table表格展示的内容
    checkInfos: {}, //目前所有的勾选的值
  },
  effects: {
    //获取表格的数据并且存放在model中
    *getList({ payload, callback }, { call, put }) {
      const response = yield call(multiFundDivision.getList, payload);
      if (response.code == 0) {
        let nwe = [];
        nwe.push(response.data.dataSource);
        let data = {
          tableList: nwe,
          columns: response.data.columns,
        };
        // let data = {
        //   tableList: [{
        //     productGroupName: '产品组',
        //     totalrate:200,
        //     actualRate: '',
        //     dValue: 200,
        //     nestedConfig: {
        //       columns: [{
        //         title: '组合名称',
        //         key: 'combName',
        //       }, {
        //         title: '组合代码',
        //         key: "combCode"
        //       }, {
        //         title: '现金余额',
        //         key: 'cashRate'
        //       }],
        //       dataSource: [{
        //         combName: '组合名称',
        //         combCode: '组合代码',
        //         cashRate: 100,
        //         children: [{
        //           combName: '组合名称1',
        //           combCode: '组合代码1',
        //           cashRate: 100,
        //         }, {
        //           combName: '组合名称1_2',
        //           combCode: '组合代码1_2',
        //           cashRate: 1200,
        //         }, {
        //           combName: '组合名称1_3',
        //           combCode: '组合代码1_3',
        //           cashRate: 1200,
        //         },{
        //           combName: '组合名称2',
        //           combCode: '组合代码2',
        //           cashRate: 1200,
        //         }]
        //       },{
        //         combName: '组合名称2',
        //         combCode: '组合代码2',
        //         cashRate: 100,
        //         children: [{
        //           combName: '组合名称12',
        //           combCode: '组合代码12',
        //           cashRate: 100,
        //         }, {
        //           combName: '组合名称1_22',
        //           combCode: '组合代码1_22',
        //           cashRate: 100,
        //         }, {
        //           combName: '组合名称1_32',
        //           combCode: '组合代码1_32',
        //           cashRate: 100,
        //         },{
        //           combName: '组合名称22',
        //           combCode: '组合代码22',
        //           cashRate: 1200,
        //         }]
        //       }]
        //     }
        //   }],
        //   columns: [{
        //     title: '产品组',
        //     key: 'productGroupName'
        //   }, {
        //     title: '汇总现金余额',
        //     key: 'totalrate'
        //   }, {
        //     title: '真实余额',
        //     key: 'actualRate'
        //   }, {
        //     title: '差值',
        //     key: 'dValue'
        //   }]
        // };
        let expandArr = [];
        let disableArr = [];
        let selectedArr = [];
        let childColumns = [];
        if (data.tableList && data.tableList.length) {
          let datas = data.tableList;
          childColumns = datas[0].nestedConfig.columns;
          for (let i = 0; i < datas.length; i++) {
            datas[i].key = uuid(4);
            datas[i].edit = true;
            datas[i].level = i + 1;
            datas[i].totalrate = datas[i].totalrate == 0 || datas[i]?.totalrate ? Number(datas[i].totalrate).toFixed(2) : null;
            expandArr.push(datas[i].key);
            if (datas[i].nestedConfig) {
              datas[i].nestedConfig.dataSource &&
                datas[i].nestedConfig.dataSource.forEach((child, inde) => {
                  child.key = uuid(6);
                  child.cashRate = child.cashRate == 0 || child.cashRate ? Number(child.cashRate).toFixed(2) : null;
                  child.level = i + 2 + '_' + (inde + 1);
                  expandArr.push(child.key);
                  disableArr.push(child.key);

                  let child_ = JSON.parse(JSON.stringify(child));
                  delete child_.children;

                  selectedArr.push(child_);
                  child.children &&
                    child.children.forEach((childs, indexs) => {
                      childs.key = uuid(8);
                      childs.cashRate = childs.cashRate == 0 || childs.cashRate ? Number(childs.cashRate).toFixed(2) : null;
                      childs.level = i + 2 + '_' + (inde + 1) + '_' + (indexs + 1);
                      expandArr.push(childs.key);
                    });
                });
            }
          }
        }
        callback({
          tableList: data.tableList,
          columns: data.columns,
          expandArr: expandArr,
          disableArr: disableArr,
          selectedArr: selectedArr,
          childColumns: childColumns,
        });
        yield put({
          type: 'save',
          payload: {
            tableList: data.tableList,
            columns: data.columns,
          },
        });
      } else {
        message.error('查询列表失败!');
        return;
      }
    },
    //确认划款
    *comfirmDivide({ payload, callback }, { call, put }) {
      const response = yield call(multiFundDivision.comfirmDivide, payload);
      if (response.code == 0) {
        callback(true);
      } else {
        message.error('确认划款失败!');
        return false;
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
